package com.mrjy.activity.bo;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mrjy.activity.dao.IActivityApplyInfoDao;
import com.mrjy.activity.dao.IActivityInfoDao;
import com.mrjy.activity.dao.IActivitySignupInfoDao;
import com.mrjy.activity.dto.ActivityApplyDTOMap;
import com.mrjy.common.entity.ActivityApplyInfo;
import com.mrjy.common.entity.ActivityInfo;
import com.mrjy.common.entity.ActivitySignupInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.enums.ResultEnum;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.ResultUtil;
@Service("activityApplyInfoBo")
public class ActivityApplyInfoBo implements IActivityApplyInfoBo{
	@Resource
	private IActivityInfoDao activityInfoDao;
	@Resource
	private IActivitySignupInfoDao activitySignupInfoDao;
	@Resource
	private IActivityApplyInfoDao activityApplyInfoDao;

	@Override
	public Object insertActivityApplyInfo(ActivityApplyInfo activityApplyInfo,
			HttpSession session) {
		// TODO Auto-generated method stub
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null == loginUser ){
			map.put("err_msg", ResultEnum.NOT_LOGIN.getMessage());
			map.put("err_code", -1);
			return map;
		}
		//查询活动是否存在
		ActivityInfo activityInfo =  activityInfoDao.selectById(activityApplyInfo.getActivityId());
		if(activityInfo == null){
			map.put("err_code", 1001);
			map.put("err_msg", "无法找到活动相关信息");
			return map;
		}else{
			//去活动支付表查询信息
			if(activityApplyInfo.getActivityPayId() == 0){
				map.put("err_code", 201);
				map.put("err_msg", "无法找到支付订单相关信息");
				return map;
			}else{
				ActivitySignupInfo actiSignupInfo = new ActivitySignupInfo();
				actiSignupInfo.setUserId(loginUser.getId());
				actiSignupInfo.setPaymentId(activityApplyInfo.getActivityPayId());
				ActivitySignupInfo activitySignupInfo = activitySignupInfoDao.queryActivityInfo(actiSignupInfo);
				if(activitySignupInfo != null){//该用户订单信息存在
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				    String time = df.format(new Date()); 
				    Timestamp ts = Timestamp.valueOf(time);
				    activityApplyInfo.setCreateTime(ts);
				    activityApplyInfo.setUserId(loginUser.getId());
				    int result = activityApplyInfoDao.insertActivityApplyInfo(activityApplyInfo);
					if(result > 0){
						map.put("err_msg", "插入成功");
						map.put("err_code",200);
					}
				}else{
					map.put("err_msg", "该用户订单信息不存在");
					map.put("err_code",202);
					return map;
				}
			}
		}
		return map;
	}

	@Override
	public Object selectActivityApplyList(ActivityApplyInfo activityApplyInfo) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		int count = activityApplyInfoDao.countActivityApplyList(activityApplyInfo);
		if(count>0){
			List<ActivityApplyDTOMap> activityApplyDTOMaps = activityApplyInfoDao.selectActivityApplyList(activityApplyInfo);
			map.put("total", count);
			map.put("err_code", 200);
			map.put("err_message", "查询成功有数据");
			map.put("rows", activityApplyDTOMaps);
			return map;
		}else{
			map.put("total", 0);
			map.put("err_code", 201);
			map.put("err_message", "查询成功无数据");
			return map;
		}
	}

	@Override
	public Object exportActivityApplyInfo(ActivityApplyDTOMap selectDTO,
			HttpServletResponse response) {
		List<ActivityApplyDTOMap> dataset = activityApplyInfoDao.selectActivityApplyList(selectDTO);
		if (CollectionUtils.isEmpty(dataset)) {
			return ResultUtil.error(ResultEnum.NO_DATA_EXPORT);
		}
		//封装数据
		String type = "";
		try {
			ExcelUtil.writeExcel(response, "活动"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", dataset, ActivityApplyDTOMap.class);
			return ResultUtil.success();
		} catch (Exception e) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("err_code", ResultEnum.ERROR.getCode());
			map.put("err_code", ResultEnum.ERROR.getMessage());
			return  map;
		}
	}

	@Override
	public Object selectByActPayId(ActivityApplyInfo info) {
		return ResultUtil.success(activityApplyInfoDao.selectByActPayId(info));
	}

	@Override
	public Object txUpdateById(ActivityApplyInfo info) {
		int count= activityApplyInfoDao.updateById(info) ;
		if(count ==1){
			return ResultUtil.success();
		}else{
			return ResultUtil.error();
		}
		
	}}
